<?php

namespace App\Http\Controllers\Api;

use App\Models\User;
use App\Http\Controllers\Controller;
use App\Library\Yunxin\Yunxin;

/**
 * 云信测试.
 */
class PlanController extends Controller
{
    public function yx_add()
    {
        $yx_model = new Yunxin();
        $list = User::orderBy('id', 'asc')->where('yx_status', 1)->take(10)->get()->toArray();

        if (count($list) < 1) {
            return false;
        }

        foreach ($list as $k => $v) {
            $icon = $v['headimg'];
            $yx_register = $yx_model->createUserIds($v['id'], $v['nickname'], '', $icon, '123456', 1);

            if (count($yx_register) > 0) {
                if ($yx_register['code'] == 200) {
                    $re = User::where('id', $v['id'])->update(['yx_status' => 2]);
                    if ($re !== false) {
                        $data['s'][] = $v['id']; //注册成功
                    } else {
                        $data['ee'][] = $v['id']; //注册成功云信注册状态更改失败
                    }
                } else {
                    if ($yx_register['desc'] == 'already register') {
                        $re = User::where('id', $v['id'])->update(['yx_status' => 2]);
                        $data['ss'][] = $v['id']; //已经注册了
                    } else {
                        $data['e'][] = $v['id']; //注册其他问题
                    }
                }
            } else {
                $data['eee'][] = $v['id']; //请求注册云信url没返回
            }
        }

        return $data;
    }
}
